package com.shenfa.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.bean.Person;
import com.shenfa.bean.Template;
import com.shenfa.bean.TemplateImage;
import com.shenfa.bean.Template_Sub;
import com.shenfa.service.TemplateService;
import com.shenfa.tools.SteelDate;

import com.shenfa.util.ResultObject;
import com.shenfa.util.sequence.SequenceUtil;

@SuppressWarnings("serial")
public class TemplateAction extends BaseAction {

	@Autowired
	private TemplateService templateService;
	
	@Autowired
	private SequenceUtil sequenceUtil;

	private static Logger log=Logger.getLogger(TemplateAction.class);
	
	private Template template;

	private Template_Sub templateSub;

	private TemplateImage image;

	private String cardid;// 卡产品名称

	private String version = "";

	private List<Template_Sub> listTemplateSub;

	private List<TemplateImage> listImg;

	private HttpServletRequest request;
	
	private int start;
	
	private int limit;

	/**
	 * 根据对象条件查询模板
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		if(null!=template){
			log.debug(template.toString()+",start="+start+",limit="+limit);
		}       
        if("".equals(template.getType()))
        {
        	template.setType(null);
        }
        if("".equals(template.getState()))
		{
			template.setState(null);
		}
		List<Template> list = templateService.getList(template,start,limit);
		result = ResultObject.build("0", list);
		result.put("total", templateService.queryTotalLen(template));
		return SUCCESS;
	}

	/**
	 * 根据对象条件查询模板,模板管理
	 */
	@SuppressWarnings("unchecked")
	public String NotVerifyList() {
		if(null!=template){
			log.debug(template.toString()+",start="+start+",limit="+limit);
		}
		if("".equals(template.getType()))
		{
			template.setType(null);
		}
		if("".equals(template.getState()))
		{
			template.setState(null);
		}
		List<Template> list = templateService.getNotVerifyList(template, start, limit);
		result = ResultObject.build("0", list);
		result.put("total", templateService.queryNotStateLen(template));
		return SUCCESS;
	}
	
	/**
	 * 根据对象条件查询模板,模板审核管理
	 */
	@SuppressWarnings("unchecked")
	public String VerifyList() {
		if(null!=template){
			log.debug(template.toString()+",start="+start+",limit="+limit);
		}
		if("".equals(template.getType()))
		{
			template.setType(null);
		}
		List<Template> list = templateService.getVerifyList(template, start, limit);
		result = ResultObject.build("0", list);
		result.put("total", templateService.queryStateLen(template));
		return SUCCESS;
	}
	
	/**
	 * 根据卡产品名称查询最新版本号
	 */
	public String selectVersion() {
		if(null!=template){
			log.debug(template.toString());
		}
		List<Template> list = new ArrayList<Template>();
		template = templateService.getLastVersion(template);
		list.add(template);
		result = ResultObject.build("0", list);
		return SUCCESS;
	}

	/**
	 * 构造模板id
	 */
	public String DoTempid() {
		List<Template> list = new ArrayList<Template>();
		template = new Template();
		template.setId(sequenceUtil.getSequence(SequenceUtil.SEQ_STENCIL));
		log.debug("DoTempid:"+template.toString());
		list.add(template);
		result = ResultObject.build("0", list);
		return SUCCESS;
	}
	
	/**
	 * 查询所有模板
	 */
	public String listAll() {
		log.debug(template.toString());
		List<Template> list = templateService.getAlllist(template);
		result = ResultObject.build("0", list);
		return SUCCESS;
	}

	/**
	 * 添加模板
	 * 
	 * @return
	 */
	public String add() {
		if(null!=template){
			log.debug(template.toString());
		}
		if(null!=templateSub){
			for (int i = 0; i < listTemplateSub.size(); i++) {
				log.debug(templateSub.toString());
			}
		}	
		
		request = ServletActionContext.getRequest();
		Person currentPerson = (Person) request.getSession().getAttribute("login");

		template.setPersonid(currentPerson.getId());
		template.setCreatetime(SteelDate.getDate(SteelDate.YYYY_MM_DD_HH_MM_SS,new Date()));
		int type = templateService.add(template);
		//循环添加对应的所有从表信息
		if (null != listTemplateSub) {
			for (Template_Sub res:listTemplateSub) {
				res.setStencil_no(template.getId());
				res.setVersion(template.getVersion());
				templateService.addTemplateSub(res);
			}
		}
		// 然后添加所有的图样信息
		if(null!=listImg){
			for(TemplateImage res:listImg){
				res.setTemp_no(template.getId());
				templateService.addTempImage(res);
			}
		}
		if (type == 1) {
			result = ResultObject.build(String.valueOf(type), "添加成功!");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败!");
		}
		return SUCCESS;
	}

	/**
	 * 删除模板
	 */
	public String del() {
		log.debug(template.toString());
		int type = templateService.del(template);
		if (type == 1) {
			result = ResultObject.build(String.valueOf(type), "删除成功!");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败!");
		}
		return SUCCESS;
	}

	/**
	 * 修改模板
	 * 
	 * @return
	 */
	public String upd() {
		if(null!=template){
			log.debug(template.toString());
		}
		if(null!=templateSub){
			for (int i = 0; i < listTemplateSub.size(); i++) {
				log.debug(templateSub.toString());
			}
		}
		
		//request = ServletActionContext.getRequest();
		//Person currentPerson = (Person) request.getSession().getAttribute("login");

		//template.setPersonid(currentPerson.getId());

		//template.setCreatetime(SteelDate.getDate(SteelDate.YYYY_MM_DD_HH_MM_SS,new Date()));
		int type = templateService.upd(template);
		// 先删除模板主表的所有从表信息
		delTemplateSub(template);
		// 循环添加模板的从表信息
		if (null != listTemplateSub) {
			for (Template_Sub res:listTemplateSub) {
				res.setStencil_no(template.getId());
				res.setVersion(template.getVersion());
				templateService.addTemplateSub(res);
			}
		}
		// 先删除所有模板的图样信息
		templateService.delTempImage(template.getId());
		// 然后添加所有的图样信息
		if(null!=listImg){
			for(TemplateImage res:listImg){
				res.setTemp_no(template.getId());
				templateService.addTempImage(res);
			}
		}
		if (type == 1) {
			result = ResultObject.build(String.valueOf(type), "修改成功!");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败!");
		}
		return SUCCESS;
	}

	/**
	 * 修改模板状态
	 * 
	 * @return
	 */
	public String updState() {
		log.debug(template.toString());
		request = ServletActionContext.getRequest();
		//Person currentPerson = (Person) request.getSession().getAttribute("login");

		//template.setPersonid(currentPerson.getId());

		//template.setCreatetime(SteelDate.getDate(SteelDate.YYYY_MM_DD_HH_MM_SS,new Date()));
		if("1".equals(template.getState())){//设置为启用，说明审核管理操作
			List<Template> list = templateService.queryState(template);
			for(Template res:list){
				res.setState("0");//设置为退回状态
				templateService.upd(res);
			}
		}
		int type = templateService.upd(template);//每种卡产品只有一个启用状态的模板
		
		if (type == 1) {
			result = ResultObject.build(String.valueOf(type), "操作成功!");
		} else {
			result = ResultObject.build(String.valueOf(type), "操作失败!");
		}
		return SUCCESS;
	}
	
	/**
	 * 根据模板编号查询对应的模板从表信息
	 * 
	 * @return
	 */
	public String listTemplateSub() {
		if(null!=templateSub){
			log.debug(templateSub.toString());
		}
		List<Template_Sub> list = templateService.getTempSubList(templateSub);
		result = ResultObject.build("0", list);
		return SUCCESS;
	}


	/**
	 * 删除匹配模板编号和版本号的所有模板从表信息
	 * 
	 * @param stencil_no
	 * @param in_no
	 * @param version
	 * @return
	 */
	public String delTemplateSub(Template template) {
		templateService.delTemplateSub(template);
		return SUCCESS;
	}

	/**
	 * 查询图样信息表
	 * 
	 * @return
	 */
	public String queryImage(){
		log.debug(template.toString());
		List<TemplateImage> list = templateService.queryImage(template.getId());
		result = ResultObject.build("0", list);
		return SUCCESS;
	}
	
	public Template getTemplate() {
		return template;
	}

	public void setTemplate(Template template) {
		this.template = template;
	}

	public TemplateService getTemplateService() {
		return templateService;
	}

	public void setTemplateService(TemplateService templateService) {
		this.templateService = templateService;
	}

	public Template_Sub getTemplateSub() {
		return templateSub;
	}

	public void setTemplateSub(Template_Sub templateSub) {
		this.templateSub = templateSub;
	}

	public List<Template_Sub> getListTemplateSub() {
		return listTemplateSub;
	}

	public void setListTemplateSub(List<Template_Sub> listTemplateSub) {
		this.listTemplateSub = listTemplateSub;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getCardid() {
		return cardid;
	}

	public void setCardid(String cardid) {
		this.cardid = cardid;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public TemplateImage getImage() {
		return image;
	}

	public void setImage(TemplateImage image) {
		this.image = image;
	}

	public List<TemplateImage> getListImg() {
		return listImg;
	}

	public void setListImg(List<TemplateImage> listImg) {
		this.listImg = listImg;
	}

	
	public void setStart(int start) {
		this.start = start;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	

}
